Stiltext hinzufügen
Die Funktion "Text stilisieren hinzufügen" in IronWord ermöglicht es Entwicklern, verschiedene Textstiloptionen anzuwenden, während sie Inhalte zu einem DOCX-Dokument hinzufügen. Sie bietet eine fein abgestimmte Kontrolle über das Erscheinungsbild des Textes, wie z. B. die Angabe der Schriftfamilie, Größe, Farbe und Stilattributen wie fett, kursiv, unterstrichen und durchgestrichen. Durch die Erstellung eines Run-Objekts, das TextContent enthält, und die Zuweisung eines TextStyle-Objekts zu Run können Entwickler die Darstellung einzelner Textelemente innerhalb eines Dokuments anpassen und so ein individuelles Erscheinungsbild gewährleisten.
Diese Funktion ist nützlich für die dynamische Erstellung professionell formatierter Dokumente, wie Berichte oder Briefe, bei denen bestimmte Abschnitte unterschiedliche Stile erfordern. Die Klasse TextStyle ermöglicht die einfache Manipulation dieser Attribute und somit sowohl einfache als auch komplexe Formatierungen innerhalb desselben Dokuments.
Wichtige Punkte
-
Einen stilvollen Lauf erstellen :
- A
Runobject is created containingTextContentwith the desired text. - The
Styleproperty of theRunis assigned aTextStyleobject to apply formatting.
- A
-
Konfigurieren des Textstils :
FontSize: Set at theTextStylelevel (not insideFont) to specify text size.TextFont: Contains font properties includingFontFamilyfor font selection.Color: Specifies the text color usingIronWord.Models.Color.IsBoldandIsItalic: Boolean properties for bold and italic formatting.Underline: Adds underline styling to the text.Strike: Applies strikethrough formatting usingStrikeValueenum.
- Hinzufügen zum Dokument :
- Use
AddChildto add the styledRunto aParagraph. - The paragraph is then added to the document with
AddParagraph.
- Use
Code Erklärung
Dieser Code zeigt, wie man Text in einem DOCX-Dokument mit IronWord erstellt und stilisiert. It begins by initializing a new WordDocument object, representing the document to be generated. A Run object is created containing TextContent with the string "Styled text example" and a TextStyle is applied to the Run to configure the appearance of the text.
The TextStyle includes settings for font size set at the TextStyle level (not inside Font), font family configured via TextFont, text color, and bold formatting. Diese Einstellungen legen fest, wie der Text im endgültigen Dokument angezeigt wird.
After the Run is styled, the AddChild method adds the Run object to a paragraph in the document. Diese Methode fügt die stilisierten Inhalte im entsprechenden Format in das Word-Dokument ein. Finally, the SaveAs method is called to export the document as "styled_document.docx". Das Ergebnis ist ein Word-Dokument, in dem der eingefügte Text gemäß den angegebenen Stilen formatiert ist und alle Schrift- und Formatierungsattribute in der Ausgabedatei beibehalten werden.
Weitere Gestaltungstipps finden Sie in den Tutorials zu Dokumentelementen.

